﻿/***********************************************************************
 * <copyright file="DataContextFactory.cs" company="BUCA JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   LinhMC
 * Email:    linhmc@buca.vn
 * Website:
 * Create Date: Thursday, October 17, 2013
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/
using System.Configuration;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace DataObjects.LinqToSql
{
    /// <summary>
    /// DataContext factory caches the connectionstring and the 
    /// mapping source so that DataContext instances can be created quickly.
    /// This significantly reduces the DataContext creation times. 
    /// </summary>
    public static class DataContextFactory
    {
        private static readonly string ConnectionString;
        private static readonly MappingSource MappingSource;

        /// <summary>
        /// Static constructor.
        /// </summary>
        /// <remarks>
        /// Static initialization of connectionstring and mappingSource.
        /// This significantly increases performance, primarily due to mappingSource cache.
        /// </remarks>        
        static DataContextFactory()
        {
            string connectionStringName = ConfigurationManager.AppSettings.Get("ConnectionStringName");
            ConnectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
            
        //    DataContext context = new BigTimeDataContext(ConnectionString);
         //   MappingSource = context.Mapping.MappingSource;
        }

        ///// <summary>
        ///// Factory methods. Creates a new DataContext using cached connectionstring.
        ///// </summary>
        ///// <returns></returns>
        //public static BigTimeDataContext CreateContext()
        //{
        //    return new BigTimeDataContext(ConnectionString, MappingSource);
        //}
    }
}
